Vue3 您所在的位置:网站首页 板报设计 样式怎么写好看 Vue3

Vue3

2024-07-11 06:52| 来源: 网络整理| 查看: 265

完整原文地址见简书https://www.jianshu.com/p/cdbd2670e075 本文内容提要

Class样式写法

常规的样式使用写法使用v-bind的形式动态设定DOM组件样式使用v-bind + Object的形式组织样式 绑定DOM组件;使用v-bind + 数组的形式组织样式 绑定DOM组件数组形式中混合Object形式的;

子组件样式 默认跟随 父组件

子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染 拥有“两个以上最外层组件”的样式处理 解决办法1,外层组件 各自配置样式;解决办法2,使用:class="$attrs.class"对外层组件进行配置, 使得统一跟随引用处样式配置;

行内样式写法

常规写法Vue式写法,使用v-bind配合dataObject形式描述样式,可读性更高 Class样式写法

常规的样式使用写法:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } const app = Vue.createApp({ template: ` luelueluelielielie` }); const vm = app.mount('#heheApp');

效果:

使用v-bind的形式动态设定DOM组件样式:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } const app = Vue.createApp({ data() { return { colorString:'blue', } }, template: ` luelueluelielielie` }); const vm = app.mount('#heheApp'); 改变data字段可以动态改变组件颜色:

使用Object的形式组织样式 绑定DOM组件:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } const app = Vue.createApp({ data() { return { colorObject: {blue:true, green:true} } }, template: ` luelueluelielielie` }); const vm = app.mount('#heheApp');

关键代码:

data() { return { colorObject: {blue:true, green:true} } },

如果将颜色键值设置成false,则网页DOM组件便不会展示:

const app = Vue.createApp({ data() { return { colorObject: {blue:true, green:false} } }, template: ` luelueluelielielie` }); const vm = app.mount('#heheApp');

使用数组的形式组织样式 绑定DOM组件:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; } .yellow { color: yellow; } const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', 'orange', 'yellow'] } }, template: ` luelueluelielielie` }); const vm = app.mount('#heheApp');

数组形式中混合Object形式的:

const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', {orange:false, yellow:true}] } }, template: ` luelueluelielielie` }); const vm = app.mount('#heheApp'); 子组件样式 默认跟随 父组件

例程: 添加子组件testDom到根组件,子组件样式没有配置,则默认跟随根组件:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; } const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', {orange:true}] } }, template: ` luelueluelielielie ` }); app.component('testDom', { template: ` heheda` }); const vm = app.mount('#heheApp');

子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染:

const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', {orange:true}] } }, template: ` luelueluelielielie ` }); app.component('testDom', { template: ` heheda` }); const vm = app.mount('#heheApp'); 拥有“两个以上最外层组件”的样式处理

不过当添加的子组件的template中,最外层有两个以上的组件的时候, 在引用子组件处(如下代码中的)配置样式是没有作用, 子组件template下的组件 会沿用根组件的样式(如下代码中的), 因为引用处配置的样式 或者其他属性, 指代的是testDom组件的最外层组件的样式 或者其他属性, 但是此时最外层组件有两个, 于是这个样式class='green'配置不知道该分配给哪个最外层组件,便失效:

const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', {orange:true}] } }, template: ` luelueluelielielie ` }); app.component('testDom', { template: ` heheda heheda ` }); const vm = app.mount('#heheApp');

解决办法1,各自配置样式:

const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', {orange:true}] } }, template: ` luelueluelielielie ` }); app.component('testDom', { template: ` heheda heheda ` }); const vm = app.mount('#heheApp');

解决办法2,使用:class="$attrs.class"对外层组件进行配置, 将自定义子组件 template下的组件 的样式, 跟随 子组件添加处(如下代码中的)配置的样式:

const app = Vue.createApp({ data() { return { colorArray: ['blue', 'green', {orange:true}] } }, template: ` luelueluelielielie ` }); app.component('testDom', { template: ` heheda heheda ` }); const vm = app.mount('#heheApp'); 行内样式写法

常规写法:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; } const app = Vue.createApp({ template: ` luelueluelielielie ` }); const vm = app.mount('#heheApp');

Vue式写法,使用v-bind配合data, 老规矩,bind后接左边一个组件属性style,右边一个data字段styleString:

Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; } const app = Vue.createApp({ data() { return { styleString:'color:blue;' } }, template: ` luelueluelielielie ` }); const vm = app.mount('#heheApp'); Object形式描述样式

当然以上是string方式描述样式的方式, 更多时候我们使用Object的形式描述样式,可读性更高 如下例程,styleString和styleObject两个字段, 分别代表以上两种描述方式,相形见绌:

const app = Vue.createApp({ data() { return { styleString:'color:blue; background: orange', styleObject: { color: 'blue', background:'orange' } } }, template: ` luelueluelielielie ` }); const vm = app.mount('#heheApp');


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有